package com.sita.tingterest.fragment;


import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.sita.bike.R;
import com.sita.tingterest.utils.DurationBreak;
import com.sita.tingterest.utils.L;

/**
 * A simple {@link Fragment} subclass.
 */
public class MusicFragment extends BaseFragment2 implements View.OnClickListener, View.OnTouchListener {

    private ImageButton buttonPlayPause, buttonnext, buttonprev;

    private SeekBar seekBarProgress;
    private String SongUri;
    boolean mServiceBound = false;
    public EditText editTextSongURL;
    private String SourceData;
    private static int CurrentPostion;
    private TextView music_currwnt_position, music_Total_length;
    long current_position_in_miliseconds;
    private long mediaFileLengthInMilliseconds; // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class
    private final Handler handler = new Handler();

    public MusicFragment() {

    }

    @Override
    protected void onChange(int position) {

    }

    @Override
    protected void onPublish(int progress) {

        L.l("MY sr", "onPublish(int progress)" + "  .. mediaFileLengthInMilliseconds" + mediaFileLengthInMilliseconds);
        seekBarProgress.setProgress((int) (((float) progress / mediaFileLengthInMilliseconds) * 100));
        current_position_in_miliseconds = (int) (((float) progress / mediaFileLengthInMilliseconds) * 100);
        //    music_Total_length.setText(DurationBreak.getDurationBreakdown( mediaFileLengthInMilliseconds).toString());
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        SongUri = getArguments().getString("SongUri");

        View layout = inflater.inflate(R.layout.ting_fragment_music2, null);
        setupViews(layout);
        L.l("MY TAG", "onCreateView " + "  ..Music Fragment;");
        return layout;

    }


    @Override
    public void onStart() {

        L.l("MY TAG", "onStart() " + "  ..Music Fragment;");
        super.onStart();
    }

    private void setupViews(View layout) {
        buttonPlayPause = (ImageButton) layout.findViewById(R.id.btn_desk_play_pause);
        buttonnext = (ImageButton) layout.findViewById(R.id.btn_desk_next);
        buttonprev = (ImageButton) layout.findViewById(R.id.btn_desk_prev);
        buttonPlayPause.setOnClickListener(this);
        buttonnext.setOnClickListener(this);
        buttonprev.setOnClickListener(this);
        seekBarProgress = (SeekBar) layout.findViewById(R.id.seekbar_desk_play);
        seekBarProgress.setMax(99); // It means 100% .0-99
        seekBarProgress.setOnTouchListener(this);
        music_currwnt_position = (TextView) layout.findViewById(R.id.music_current_postion);
        music_Total_length = (TextView) layout.findViewById(R.id.music_total_length);

    }


    @Override
    public void onResume() {
        super.onResume();
        allowBindService();
        //   music_currwnt_position .setText(DurationBreak.getDurationBreakdown(current_position_in_miliseconds));


    }

    @Override
    public void onPause() {

        super.onPause();
        allowUnbindService();

    }

    @Override
    public void onStop() {

        L.l("MY TAG", "    onStop()  ;MusicFragment; ");

        super.onStop();
    }


    /**
     * 播放 or 暂停
     *
     * @param
     */

    public void pre(View view) {

        mPlayService.pre(); // 上一曲
    }

    /**
     * 播放 or 暂停
     *
     * @param
     */
    public void play(View view) {
        if (mPlayService.isPlaying()) {
            mPlayService.pausePlayer(); // 暂停+


            //   mCdView.pause();
            buttonPlayPause.setImageResource(R.drawable.player_btn_play_normal);
        } else {
            onPlay(mPlayService.getCurrentPosition()); // 播放
        }
    }

    private void onPlay(int position) {
//        Music music = MusicUtils.sMusicList.get(position);

        //  mMusicTitle.setText(music.getTitle());
        //  mSingerTextView.setText(music.getArtist());
        //  mPlaySeekBar.setMax(music.getLength());
        //  Bitmap bmp = MusicIconLoader.getInstance().load(music.getImage());
        //  if(bmp == null) bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        //  mCdView.setImage(ImageTools.scaleBitmap(bmp, (int)(App.sScreenWidth * 0.7)));

    /*    if(mPlayService.isPlaying()) {
          //  mCdView.start();
            buttonPlayPause.setImageResource(R.drawable.player_btn_pause_normal);
        }else {
         //   mCdView.pause();
            buttonPlayPause.setImageResource(R.drawable.player_btn_play_normal);
        }*/

        if (!mPlayService.isPlaying()) {
            //  mCdView.start();
            mPlayService.play(mPlayService.getCurrentPosition());
            mediaFileLengthInMilliseconds = mPlayService.getDuration();
            music_Total_length.setText(DurationBreak.getDurationBreakdown(mediaFileLengthInMilliseconds).toString());

            //   int mediaFileLengthInMilliseconds = mPlayService.getDuration();
            //     Log.v("MY YAG", " mediaFileLengthInMilliseconds  " + mediaFileLengthInMilliseconds);
            // primarySeekBarProgressUpdater();
            buttonPlayPause.setImageResource(R.drawable.player_btn_pause_normal);
        } else {

            mPlayService.pausePlayer();
            //   mCdView.pause();
            buttonPlayPause.setImageResource(R.drawable.player_btn_play_normal);
        }

    }


    public void next(View view) {
        L.l("MY TAG", "     next(View view)  MusicFragment ; ");
        mPlayService.next(); // 上一曲
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_desk_play_pause:
                play(v);
                break;
            case R.id.btn_desk_next:
                next(v);
                Toast.makeText(getActivity(), "press Next()", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_desk_prev:
                pre(v);
                Toast.makeText(getActivity(), "press previous", Toast.LENGTH_SHORT).show();
                break;


        }

    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v.getId() == R.id.seekbar_desk_play) {
            if (v.getId() == R.id.play_progress) {
                if (mPlayService.isPlaying()) {
                    SeekBar sb = (SeekBar) v;
                    int playPositionInMillisecconds = ((int) mediaFileLengthInMilliseconds / 100) * sb.getProgress();
                    mPlayService.seek(playPositionInMillisecconds);
                }
            }
        }
        return false;

    }


}
